var app = require('express').createServer();
var mysql = require('mysql');
var client = mysql.createClient({
  user: 'root',
  password: 'mysql',
  host : '10.10.4.29',
  port : 3306
});

client.database = 'game_7road';

app.get(/^\/news(?:\/(\d+))/, function(req, res){
  var id = req.params[0];
  if(id)
  {
    sql = 'SELECT * FROM ddt_news WHERE news_id = '+ id;
    console.log(sql);
    client.query(sql, function selectCb(err, results, fields) {
        if (err) {
          throw err;
        }
        res.send(results);
        //console.log(results);
        //console.log(fields);
        //client.end();
      }
    );
  }
});

app.get(/^\/news-pager(?:\/(\d+)(?:\/(\d+))?)/, function(req, res){
  var pageNum = req.params[0]-1;
  var pageSize = req.params[1];
  if(!pageSize)
  {
    pageSize = 10;
  }
  sql = 'SELECT * FROM ddt_news ORDER BY news_id DESC LIMIT ' + pageNum+','+pageSize;

  console.log(sql);
  client.query(sql, function selectCb(err, results, fields) {
      if (err) {
        throw err;
      }
      res.send(results);
    }
  );

});


app.listen(8000);